package com.neusoft.neuiotms.dcom.converter;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.bizcore.web.utils.id.BaseIdGenerator;
import com.neusoft.neuiotms.dcom.dto.ClassItemDTO;
import com.neusoft.neuiotms.dcom.model.ClassItem;

@Component
public class ClassItemConverter extends AbstractConverter<ClassItem, ClassItemDTO> {

    @Autowired
    private BaseIdGenerator classesItemCodeGenerator;

    @Override
    public ClassItem toModel(final ClassItemDTO dto) {

        final ClassItem model = new ClassItem();

        model.setName(dto.getName());
        model.setStartTime(dto.getStartTime());
        model.setEndTime(dto.getEndTime());
        model.setCycle(dto.getCycle());

        if (StringUtils.isEmpty(dto.getCode())) {
            model.setCode(this.classesItemCodeGenerator.generateCode());
        } else {
            model.setCode(dto.getCode());
        }
        return model;
    }

    @Override
    public ClassItemDTO toDTO(final ClassItem model, final boolean forListView) {

        final ClassItemDTO dto = new ClassItemDTO();

        dto.setId(model.getId());
        dto.setCode(model.getCode());
        dto.setStartTime(model.getStartTime());
        dto.setEndTime(model.getEndTime());
        dto.setName(model.getName());
        dto.setCycle(model.getCycle());

        return dto;
    }

}
